<template>
    <section>
        <!--添加和高级查询的工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.username" placeholder="请输入员工名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="filters.state" placeholder="选择部门状态">
                        <el-option value="1" key="1" label="启用"></el-option>
                        <el-option value="0" key="0" label="禁用"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="loadEmployee(1)">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <!--departments：列表绑定的数据
            highlight-current-row:单选
            v-loading：显示加载框
            @selection-change：操作复选框触发的事件(方法)
            type="index"： 自动有编号，从1开始
        -->
        <el-table :data="employees" v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="80" label="编号">
            </el-table-column>
            <el-table-column prop="username" label="账号">
            </el-table-column>
            <el-table-column prop="password" label="密码">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column prop="phone" label="电话号">
            </el-table-column>
            <el-table-column prop="department.name" label="所属部门">
            </el-table-column>
            <el-table-column prop="age" label="年龄">
            </el-table-column>
            <el-table-column prop="state" label="部门状态" :formatter="formatState">
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具栏-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove(sels)" :disabled="this.sels.length==0">批量删除</el-button>
            <el-pagination style="float: right" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page="currentPage"
                           :page-sizes="pageSizes"
                           :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </el-col>

        <!--模态框 -->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="editForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话号" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model.number="editForm.age" auto-complete="off"></el-input>
                    <!--<el-input type="age" v-model.number="numberValidateForm.age" autocomplete="off"></el-input>-->
                </el-form-item>
                <el-form-item label="部门状态">
                    <el-radio-group v-model="editForm.state">
                        <el-radio class="radio" :label="1">开启</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!--Departments：返回所有所部门的集合-->
                <el-form-item label="所属部门">
                    <el-select v-model="editForm.departmentId" placeholder="请选择">
                        <el-option
                                v-for="item in parentDepartments"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>
<script>
    export default {
        data() {
            return {
                //模态框默认为false
                editFormVisible:false,
                //模态框的内容
                editForm: {
                    id:"",
                    username:"",
                    password:"",
                    state:"",
                    departmentId:""
                },
                //模态框的校验
                editFormRules:{
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                    age: [
                        { required: true, message: '年龄不能为空'},
                        { type: 'number', message: '年龄必须为数字值',trigger: 'blur'}
                    ],
                    phone: [
                        { required: true, message: '请输入电话', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                },
                //所属部门,(下拉框)
                parentDepartments:[],
                //高级查询的条件
                filters:{
                    username:"",
                    state:""
                },
                //批量删除
                sels: [],
                //员工信息
                employees: [],
                //默认展示为第一页
                currentPage: 1,
                //每页展示可以选5,10,15,20
                pageSizes: [5, 10, 15, 20],
                //每页展示的条数
                pageSize: 5,
                //展示的总条数
                total: 0,
                //加载框子,默认为false
                listLoading: false
            }
        },
        methods: {
            //启用禁用                              完成
            formatState(row) {

                return row.state == 1 ? "启用" : "禁用"
            },
            //批量删除                              完成
            batchRemove(sels) {
                console.debug("批量要删除的次数", sels)
                //把选中的对象当做参数传到后台
                let batchParam = sels
                this.$confirm('您确认要删除勾选的数据吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("http://localhost:80/employee/batchRemove", batchParam).then((res => {
                        if (res.data.success) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //刷新页面
                            this.loadEmployee();
                        } else {
                            this.$message('删除失败哈哈哈!!!!');
                        }
                    }));
                })
            },
            //删除                                  完成
            handleDel(index, row) {
                console.debug("要删除的id", row.id)
                this.$confirm('您确认离我而去吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("http://localhost/employee/"+row.id).then((res=>{
                        if (res.data.success) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //刷新页面
                            this.loadEmployee();
                        } else {
                            this.$message('删除失败哈哈哈!!!!');
                        }
                    }));
                })

            },
            //增加和编辑的提交按钮
            editSubmit(){
                ////当form表单要进行提交的时候，必须要验证通过
                this.$refs.editForm.validate((valid) => {
                    //如果我的校验通过就进行添加和修改
                    if (valid) {
                        //有id则修改,无id则添加
                        if(this.editForm.id){
                            if (this.editForm.departmentId){
                                this.editForm.department={
                                    "id":this.editForm.departmentId
                                }
                            }
                            let paramUpdate=this.editForm;
                            this.$http.post("http://localhost/employee",paramUpdate).then((res=>{
                                if (res.data.success) {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功!'
                                    });
                                    //刷新页面
                                    this.loadEmployee();
                                    this.editFormVisible = false
                                } else {
                                    this.$message('添加失败哈哈哈!!!!');
                                }
                            }))
                        }else{
                            if (this.editForm.departmentId){
                                this.editForm.department={
                                    "id":this.editForm.departmentId
                                }
                            }
                            let paramAdd=this.editForm;
                            this.$http.put("http://localhost/employee",paramAdd).then((res=>{
                                if (res.data.success) {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功!'
                                    });
                                    //刷新页面
                                    this.loadEmployee();
                                    this.editFormVisible = false
                                } else {
                                    this.$message('添加失败哈哈哈!!!!');
                                }
                            }))
                        }
                    }
                });
            },
            //增加
            handleAdd(){
                //1.弹出模态框
                this.editFormVisible=true;
                //2.数据清除缓存
                for (let k in this.editForm){
                    this.editForm[k]=""
                }
                this.editForm.state=1;
            },
            //编辑               有个问题？ 回显所属部门
            handleEdit(index, row) {
                console.debug("编辑",row)
                //1,弹出模态框
                this.editFormVisible=true;
                //小debug:点击编辑要清楚缓存
                for (let k in this.editForm){
                    this.editForm[k]=""
                }
                //2.数据回显
                for (let k in row){
                    this.editForm[k]=row[k];
                }
                //3.回显所属部门
                if(this.editForm.department){
                    this.editForm.departmentId=this.editForm.department.id
                }
            },
            //选中即可进行批量操作
            selsChange(val) {
                console.debug("是啥", val)
                this.sels = val;
                console.debug(this.sels)
            },
            //改变页面展示的条数                     完成
            handleSizeChange(val) {
                console.debug("每页展示:", val)
                //1.每次都是跳转到第一页
                this.currentPage = 1;
                //2.页面展示的条数
                this.pageSize = val;
                //3.加载员工列表
                this.loadEmployee();
            },
            //展示第几页                            完成
            handleCurrentChange(val) {
                console.debug("显示", val, "页")
                //1.赋值给currentPage,就显示在第几页
                this.currentPage = val;
                //2.加载员工列表
                this.loadEmployee();
            },
            //加载部门员工
            loadEmployee(val) {
                if (val){

                }
                let param = {
                    "currentPage": this.currentPage,
                    "pageSize": this.pageSize,
                    "username":this.filters.username,
                    "state":this.filters.state
                }
                this.$http.patch("http://localhost:80/employee", param).then((res => {
                    console.debug("我要去后台执行数据", res)
                    //请求之前显示加载框
                    this.listLoading = true;
                    this.employees = res.data.list;
                    this.total = res.data.total;
                    //请求之后显示加载框
                    this.listLoading = false;
                }))
            },
            //所属部门
            loaddePartment(){
                this.$http.patch("http://localhost/department/loadParentAll").then((res=>{
                    console.debug("所属部门",res)
                    this.parentDepartments=res.data;
                }))
            }
        },
        mounted() {  //入口函数
            this.loadEmployee();
            this.loaddePartment();
        }
    }

</script>